package com.ruoyi.business.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.request.VersionReq;
import com.ruoyi.business.response.VersionRes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AppVersionMapper;
import com.ruoyi.business.domain.AppVersion;
import com.ruoyi.business.service.IAppVersionService;

/**
 * App版本Service业务层处理
 *
 * @author Eric
 * @date 2024-03-29
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements IAppVersionService {

    @Override
    public VersionRes checkAppVersion(VersionReq req) {
        VersionRes res = new VersionRes();
        if (StrUtil.isEmpty(req.getAppId()) || StrUtil.isEmpty(req.getAppName()) || StrUtil.isEmpty(req.getVersionCode())
                || StrUtil.isEmpty(req.getVersionName())) {
            res.setUpdateFlag(0);
            return res;
        }
        AppVersion appVersion = this.getOne(Wrappers.lambdaQuery(AppVersion.class)
                .eq(AppVersion::getIsNew, true)
                .orderByDesc(AppVersion::getId)
                .last(" LIMIT 1"));
        if (appVersion != null) {
            // 先检查大版本
            String oldVersionName = req.getVersionName();
            String newVersionName = appVersion.getVersionName();
            List<String> oldVerList = StrUtil.split(oldVersionName, ".");
            List<String> newVerList = StrUtil.split(newVersionName, ".");
            if (oldVerList.size() != newVerList.size()) {
                res.setUpdateFlag(1);
                res.setRemark(appVersion.getRemark());
                res.setAppPath(appVersion.getAppPath());
                return res;
            }
            for (int i = 0; i < oldVerList.size(); i++) {
                int oldVer = Convert.toInt(oldVerList.get(i));
                int newVer = Convert.toInt(newVerList.get(i));
                if (NumberUtil.compare(newVer, oldVer) > 0) {
                    res.setUpdateFlag(1);
                    res.setRemark(appVersion.getRemark());
                    res.setAppPath(appVersion.getAppPath());
                    return res;
                }
            }
            // 检查小版本
            int oldVersionCode = Convert.toInt(req.getVersionCode());
            int newVersionCode = Convert.toInt(appVersion.getVersionCode());
            if (NumberUtil.compare(newVersionCode, oldVersionCode) > 0) {
                // 整包更新
                res.setUpdateFlag(2);
                res.setRemark(appVersion.getRemark());
                res.setWgtPath(appVersion.getWgtPath());
                return res;
            }
        }
        res.setUpdateFlag(0);
        return res;
    }
}
